Skip to main content

Convert Byte[] to String and Vice-versa

Banner java icon

🎭 The Great Byte[] vs. String Showdown in Java! πŸŽ­β€‹

Ever wondered how to juggle between byte[] and String in Java? Well, grab your popcorn 🍿 because this transformation trick is used in many scenarios, from IO operations to generating secure hashes! But beware... 🀨

⚠️ Unless absolutely necessary, DO NOT convert between String and byte[] willy-nilly! They serve different purposesβ€”String is for text, byte[] is for binary data. Mixing them up can cause chaos! 😱


🎩 1. From byte[] to String - Magic Unleashed! βœ¨β€‹

πŸ—οΈ 1.1. Using the String Constructor​

The simplest way to transform byte[] into String is by using the String constructor. VoilΓ ! πŸͺ„

byte[] bytes = "hello world".getBytes();
String s = new String(bytes);

This method is easy-peasy, but be careful! The default character encoding might betray you. 😏


πŸ”₯ 1.2. Using Base64 - The Secret Agent Way πŸ•ΆοΈβ€‹

Since Java 8, Base64 came to the rescue! Base64 is your best friend when encoding binary data as text. If you want to safely send bytes over text-based protocols (like emails πŸ“§ or JSON πŸ“œ), Base64 is your guy!

byte[] bytes = "hello world".getBytes();
String s = Base64.getEncoder().encodeToString(bytes);

Now your byte array is disguised as a string! πŸ¦Έβ€β™‚οΈ


πŸš€ 2. From String to byte[] - Reversing the Spell πŸ”„β€‹

βš™οΈ 2.1. Using String.getBytes() - The Classic Move πŸŽ­β€‹

If you need to turn a String into byte[], Java's getBytes() method is your best bet.

String string = "howtodoinjava.com";
byte[] bytes = string.getBytes();

⚠️ Beware! This method uses the default charset of your system, which may not be what you expect! Specify a charset explicitly if needed. 🎯


πŸ•΅οΈ 2.2. Using Base64 - The Reverse Heist πŸ΄β€β˜ οΈβ€‹

If your string is Base64-encoded, you can decode it back into byte[] using:

String string = "howtodoinjava.com";
byte[] bytes = Base64.getDecoder().decode(string);

Oops! If string wasn’t actually encoded in Base64, this will explode πŸ’₯ with an exception! Handle with care. πŸ€–


🎯 3. Summary - The Grand Finale πŸŽ¬β€‹

So, what did we learn today? πŸ€”

βœ… Use String class when dealing with plain text.

βœ… Use Base64 when dealing with binary data disguised as text.

βœ… Be mindful of character encoding to avoid unexpected surprises. 🎭

Got questions? Drop them in the comments! πŸ“©

πŸ”₯ Happy Learning & Keep Coding! πŸš€